<?php
global $out;
class ModuleTopMenu extends Controller { 

	function fetch() {
		$config   =& $this->locator->get('config');
		$language =& $this->locator->get('language');
		$url      =& $this->locator->get('url');
		$user     =& $this->locator->get('user');
        $database =& $this->locator->get('database');
        $template =& $this->locator->get('template');

		if (($user->isLogged()) && ($config->get('topmenu_status'))) {

	  		$language->load('extension/module/topmenu.php');

	  		$view = $this->locator->create('template');
	  		
	  		$view->set('tpl', $template);

            global $out;
			$results = $database->getRows("SELECT a.menu_id AS menu_id, a.title AS title, a.href AS href, a.icon AS icon, COALESCE( c.menu_id, 0 ) AS parent_id ,IF(b.sort_order=0,99999,b.sort_order) AS sort_order FROM admenu a LEFT OUTER JOIN admenu b ON a.menu_id = b.menu_id LEFT OUTER JOIN admenu c ON b.parent_id = c.menu_id WHERE a.language_id = '" . (int)$language->getId() . "' ORDER BY a.sort_order;");
            
            if(isset($results)) {
                $tree = $this->getTree($results,0);
            } 

            if(isset($tree)) {
                $out .= '<ul class="menu">';
                $this->listTree($tree);
                $out .= '</ul>';
                $topmenu = $out;
            }

            $view->set('topmenu', $topmenu);

      		return $view->fetch('module/topmenu.tpl');

		}

  	}

    // Returns an associative array containing the entire category hierarchy.
    function getTree($relationships,$parent_id){
        
        global $out;

        $children = array();

        foreach($relationships as $key=>$val){

            if($val['parent_id'] == $parent_id){
                unset($relationships[$key]);
                $child = array(
                    'menu_id'  => $val['menu_id'],
                    'icon'     => $val['icon'],
                    'title'    => $val['title'],
                    'href'     => $val['href'],
                    'children' => array()
                );

                $child['children'] = $this->getTree($relationships,$val['menu_id']);

                if(count($child['children']) == 0){
                    unset($child['children']);
                }
                array_push($children,$child);
            }
        }
        return $children;
    } //

   
    // Returns HTML-ised version of an associative array containing the entire category hierarchy.
    function listTree($tree=array(),$level=0){
		$template =& $this->locator->get('template');
		
        global $out; //using the global because I don't know how to keep a running variable to return
        $url =& $this->locator->get('url');

        if(!is_int($level)){
            $level = 0;
        }
        $level++;

        foreach($tree as $key=>$val){ //for each main menu...

            $out .= '<li>'; //create a list item...
            if ($val['icon'] != NULL) {
                $img = '<img src="' . $template->getImage('image/menu/' . $val['icon']) . '" alt="'.$val['icon'].'" /> ';
            } else {
                $img = "";
            }

            if ($val['href'] != NULL) { //if it has an href value, use it. Else, use "#"
                $out .= '<a href="'.$val['href'].'" title="'.$val['title'].'">'.$img.$val['title'].'</a>';
            } else {
                $out .= '<a href="javascript:void(0)" title="'.$val['title'].'">'.$img.$val['title'].'</a>';
            }

            if(isset($val['children'])){ //if it has children, get them in their own UL...
                $out .=  '<ul>' . "\r\n";
                $children = $this->listTree($val['children'],$level+1);
                $out .=  '</ul>'. "\r\n".'</li>'. "\r\n";
            } else {
                $out .= '</li>'. "\r\n";    // otherwise just close the list item
            }
        }

    }//

}
?>